package com.qiangesoft.deepseek.controller;

import io.github.pigmesh.ai.deepseek.core.DeepSeekClient;
import io.github.pigmesh.ai.deepseek.core.chat.ChatCompletionModel;
import io.github.pigmesh.ai.deepseek.core.chat.ChatCompletionRequest;
import io.github.pigmesh.ai.deepseek.core.chat.ChatCompletionResponse;
import io.github.pigmesh.ai.deepseek.core.chat.ResponseFormatType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import javax.servlet.http.HttpServletResponse;

@RestController
public class ChatController {

    @Autowired
    private DeepSeekClient deepSeekClient;

    @GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ChatCompletionResponse> chat(String userMessage, HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");
        return deepSeekClient.chatFluxCompletion(userMessage);
    }

    @GetMapping(value = "/chatC", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ChatCompletionResponse> chatC(String userMessage) {
        ChatCompletionRequest request = ChatCompletionRequest.builder()
                // 模型选择，支持 DEEPSEEK_CHAT、DEEPSEEK_REASONER 等
                .model(ChatCompletionModel.DEEPSEEK_CHAT)
                // 添加用户消息
                .addUserMessage(userMessage)
                // 添加助手消息，用于多轮对话
                .addAssistantMessage("上轮结果")
                // 添加系统消息，用于设置角色和行为
                .addSystemMessage("你是一个专业的助手")
                // 设置最大生成 token 数，默认 2048
                .maxTokens(1000)
                // 设置响应格式，支持 JSON 结构化输出
                .responseFormat(ResponseFormatType.JSON_OBJECT)
                .tools() // function calling
                .build();

        return deepSeekClient.chatFluxCompletion(request);
    }

}
